<template>
	<view class="navbar">
		<view class="navbar-fixed">
			<!-- 状态栏小程序撑起高度 -->
			<view :style="{height:statusBarHeight+'px'}"></view>
			<view class="navbar-content" :style="{height:navBarHeight+'px',width:windowWidth+'px'}">
				<view class="nav-back" @tap="goBack">
					<button class="back-btn"></button>
				</view>
				<view class="nav-bar-title" :style="{textAlign: isCenter ? 'center' : '',width: width ? 'auto': ''}">
					<text :style="bold ? 'font-weight:600;font-size:' + size + 'rpx' :''">{{ title }}</text>
				</view>
				<view class=""></view>
			</view>
		</view>
		<!-- 需要添加占位符高度  状态栏高度+导航栏高度（否则下面tab会塌陷）-->
		<view :style="{height: statusBarHeight+navBarHeight+'px'}"></view>
	</view>
</template>

<script>
	export default {
		name: 'navbar',
		props: {
			title: {
				type: String,
				default: '导航栏标题'
			},
			width:{
				type: Boolean,
				default: false
			},
			isCenter: {
				type: Boolean,
				default: false
			},
			bold: {
				type: Boolean,
				default: false
			},
			size:{
				type:Number,
				default: 28
			}
		},
		data() {
			return {
				statusBarHeight: 20,
				/* 状态栏高度 */
				navBarHeight: 45,
				/* 导航栏高度 */
				windowWidth: 375,
				/* 窗口宽度 */
				/* 设定状态栏默认高度 */
				val: '' /* 导航栏搜索框的值 */
			};
		},
		created() {
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			// 设置状态栏高度（H5顶部无状态栏小程序有状态栏需要撑起高度）
			this.statusBarHeight = info.statusBarHeight
			this.windowWidth = info.windowWidth
			// 除了h5 app mp-alipay的情况下执行
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			// 获取胶囊的位置
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			console.log(menuButtonInfo);
			// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
			this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info
				.statusBarHeight)
			this.windowWidth = menuButtonInfo.left
			// #endif
		},
		methods: {
			goBack() {
				this.$emit('back')
			}
		}
	}
</script>

<style lang="less">
	.navbar {
		.navbar-fixed {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			width: 100%;
			background-color: #fff;

			.navbar-content {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 45px;
				box-sizing: border-box;

				.back-btn {
					width: 10px;
					height: 18px;
					background-image: url("https://wx.xtwaixie.com/img/back1.png");
					background-repeat: no-repeat;
					background-size: cover;
					background-position: center center;
					border: 0;
					align-items: center;
					// transform: translateX(-170rpx);
					margin: 11px 10px 11px 12px;
				}

				.nav-back {
					height: 100%;
					width: 30px;
				}

				.nav-bar-title {
					height: 20px;
					width: 100px;
					line-height: 20px;
					margin-left: 65px;
					font-size: 28rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}

		}

	}
</style>